package open.template.work.commons.http;

import com.alibaba.fastjson.JSON;
import open.template.work.commons.exception.BaseTemplateException;
import open.template.work.commons.exception.ExceptionCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.TreeMap;

public class HttpParser {

    private static Logger LOGGER= LoggerFactory.getLogger(HttpParser.class);

    public static TreeMap<String,Object> requestJsonParser(InputStream inputStream){
        try {
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer=new StringBuffer();
            String tempStr;
            while((tempStr=bufferedReader.readLine())!=null){
                stringBuffer.append(tempStr);
            }
            LOGGER.info("解析输入流文本={}",stringBuffer.toString());

            String jsonStr=stringBuffer.toString();
            return JSON.parseObject(jsonStr, TreeMap.class);
        } catch (IOException e) {
            LOGGER.error("解析输入流出错",e);
            throw new BaseTemplateException(ExceptionCodeEnum.S99999);
        }

    }
}
